package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.dto.OrderResponse;
import com.luo.backend.service.AdminOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理员订单管理控制器
 */
@RestController
@RequestMapping("/api/v1/admin/orders")
public class AdminOrderController {
    
    private static final Logger logger = LoggerFactory.getLogger(AdminOrderController.class);
    
    @Autowired
    private AdminOrderService adminOrderService;
    
    /**
     * 获取订单列表
     * GET /api/v1/admin/orders
     */
    @GetMapping
    public ApiResponse<Map<String, Object>> getOrderList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String sortOrder) {
        try {
            logger.info("管理员获取订单列表 - page: {}, size: {}, status: {}", page, size, status);
            
            Map<String, Object> result = adminOrderService.getOrderList(
                page, size, status, keyword, startDate, endDate, sortBy, sortOrder);
            
            return ApiResponse.success(result);
        } catch (Exception e) {
            logger.error("获取订单列表失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取订单详情
     * GET /api/v1/admin/orders/{id}
     */
    @GetMapping("/{id}")
    public ApiResponse<OrderResponse> getOrderDetail(@PathVariable Long id) {
        try {
            logger.info("管理员获取订单详情 - orderId: {}", id);
            
            OrderResponse order = adminOrderService.getOrderDetail(id);
            return ApiResponse.success(order);
        } catch (Exception e) {
            logger.error("获取订单详情失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 删除订单
     * DELETE /api/v1/admin/orders/{id}
     * 限制：只能删除已取消或已退款的订单
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteOrder(@PathVariable Long id) {
        try {
            logger.info("管理员删除订单 - orderId: {}", id);
            
            adminOrderService.deleteOrder(id);
            return ApiResponse.success("订单删除成功");
        } catch (Exception e) {
            logger.error("删除订单失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 修改订单状态
     * PUT /api/v1/admin/orders/{id}/status
     */
    @PutMapping("/{id}/status")
    public ApiResponse<String> updateOrderStatus(
            @PathVariable Long id,
            @RequestBody Map<String, String> request) {
        try {
            String status = request.get("status");
            String reason = request.get("reason");
            
            if (status == null || status.trim().isEmpty()) {
                return ApiResponse.error("订单状态不能为空");
            }
            
            if (reason == null || reason.trim().isEmpty()) {
                return ApiResponse.error("修改原因不能为空");
            }
            
            logger.info("管理员修改订单状态 - orderId: {}, status: {}", id, status);
            
            adminOrderService.updateOrderStatus(id, status, reason);
            return ApiResponse.success("订单状态修改成功");
        } catch (Exception e) {
            logger.error("修改订单状态失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 获取订单统计
     * GET /api/v1/admin/orders/statistics
     */
    @GetMapping("/statistics")
    public ApiResponse<Map<String, Object>> getOrderStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            logger.info("管理员获取订单统计 - startDate: {}, endDate: {}", startDate, endDate);
            
            Map<String, Object> statistics = adminOrderService.getOrderStatistics(startDate, endDate);
            return ApiResponse.success(statistics);
        } catch (Exception e) {
            logger.error("获取订单统计失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}

